local kl__shengong = fk.CreateSkill {
  name = "kl__shengong",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable{
  ["kl__shengong"] = "神工",
  [":kl__shengong"] = "锁定技，你的回合开始时，若你的装备区没有<a href= ':role__wooden_ox'>【木牛流马】</a>，你将【木牛流马】置入你的装备区；"..
  "当【木牛流马】内牌数变化时或你使用装备牌时，你进行一次判定，若为红，你摸一张牌，若为黑，你摸两张牌并弃置两张牌。",

  ["#kl__shengong-dis"] = "神工：请弃置两张牌",

  ["$kl__shengong1"] = "以有穷之思，化无穷之变。",
}

local U = require "packages.utility.utility"

kl__shengong:addEffect(fk.TurnStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(kl__shengong.name) then
      if not table.find(player:getCardIds("e"), function(id) return Fk:getCardById(id).name == "role__wooden_ox" end) then
        local areas = { Card.Void, Card.DrawPile, Card.DiscardPile, Card.PlayerHand, Card.PlayerEquip, Card.PlayerJudge, Card.PlayerSpecial }
        local catapult = table.find(U.prepareDeriveCards(player.room, {{"role__wooden_ox", Card.Diamond, 5}}, kl__shengong.name), function (id) return table.contains(areas, player.room:getCardArea(id)) end)
        return catapult and player:canMoveCardIntoEquip(catapult)
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local areas = { Card.Void, Card.DrawPile, Card.DiscardPile, Card.PlayerHand, Card.PlayerEquip, Card.PlayerJudge, Card.PlayerSpecial }
    local catapult = table.find(U.prepareDeriveCards(room, {{"role__wooden_ox", Card.Diamond, 9}}, kl__shengong.name), function (id) return table.contains(areas, player.room:getCardArea(id)) end)
    if catapult then
      room:moveCardIntoEquip(player, catapult, kl__shengong.name, true, player)
    end
  end,
})

local spec = {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(kl__shengong.name) and data.card and data.card.type == Card.TypeEquip
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = kl__shengong.name,
      pattern = ".",
    }
    room:judge(judge)
    if judge.card.color == Card.Red then
      room:drawCards(player, 1, kl__shengong.name)
    elseif judge.card.color == Card.Black then
      room:drawCards(player, 2, kl__shengong.name)
      if not player:isNude() then
        room:askToDiscard(player, {
          min_num = 2,
          max_num = 2,
          skill_name = kl__shengong.name,
          cancelable = false,
          include_equip = true,
          prompt = "#kl__shengong-dis",
        })
      end
    end
  end,
}

kl__shengong:addEffect(fk.CardUsing, spec)

kl__shengong:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(kl__shengong.name) then
      local cards = {}
      for _, move in ipairs(data) do
        if move.specialName and move.specialName == "$role_carriage" then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(cards, info.cardId)
          end
        end
        for _, info in ipairs(move.moveInfo) do
          if info.fromSpecialName and info.fromSpecialName == "$role_carriage" then
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end
      return #cards > 0
    end
  end,
  on_use = spec.on_use,
})


return kl__shengong
